package cn.wolfcode.p2p.business.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.business.domain.AccountFlow;
import cn.wolfcode.p2p.business.mapper.AccountFlowMapper;
import cn.wolfcode.p2p.business.service.IAccountFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service@Transactional
public class AccountFlowServiceImpl implements IAccountFlowService {

    @Autowired
    private AccountFlowMapper accountFlowMapper;
    @Override
    public int save(AccountFlow accountFlow) {
        return accountFlowMapper.insert(accountFlow);
    }

    private void createFlow(Account account, BigDecimal amount, int ActionType, String note) {
        AccountFlow af = new AccountFlow();
        af.setAccountId(account.getId());
        af.setActionTime(new Date());
        af.setAmount(amount);
        af.setFreezedAmount(account.getFreezedAmount()); //冻结金额
        af.setUsableAmount(account.getUsableAmount());  //可用金额
        af.setActionType(ActionType);
        af.setNote(note);
        accountFlowMapper.insert(af);
    }

    @Override
    public void createRechargeOfflineFlow(Account account, BigDecimal amount) {
        createFlow(account, amount, Constants.ACCOUNT_ACTIONTYPE_RECHARGE_OFFLINE, "线下充值"+ amount + "元");
    }

    @Override
    public void createBidFlow(Account account, BigDecimal amount) {
        createFlow(account, amount, Constants.ACCOUNT_ACTIONTYPE_BID_FREEZED, "投标冻结"+ amount + "元");
    }

    @Override
    public void createBidFiledFlow(Account account, BigDecimal amount) {
        createFlow(account, amount, Constants.ACCOUNT_ACTIONTYPE_BID_UNFREEZED, "投标失败,取消冻结"+ amount + "元");
    }

    @Override
    public void createBorrowSuccessFlow(Account account, BigDecimal amount) {
        createFlow(account, amount, Constants.ACCOUNT_ACTIONTYPE_BIDREQUEST_SUCCESSFUL, "借款成功"+ amount + "元");
    }

    @Override
    public void createPayAccountManagementChargeFlow(Account account, BigDecimal amount) {
        createFlow(account, amount, Constants.ACCOUNT_ACTIONTYPE_CHARGE, "支付平台借款手续费"+ amount + "元");
    }

    @Override
    public void createBidSuccessFlow(Account account, BigDecimal amount) {
        createFlow(account, amount, Constants.ACCOUNT_ACTIONTYPE_BID_SUCCESSFUL, "投标成功,取消冻结"+ amount + "元");
    }

    @Override
    public void createReturnMoneyFlow(Account account, BigDecimal amount) {
        createFlow(account, amount, Constants.ACCOUNT_ACTIONTYPE_RETURN_MONEY, "还款成功"+ amount + "元");
    }

    @Override
    public void createReceiveReturnMoney(Account account, BigDecimal amount) {
        createFlow(account, amount, Constants.ACCOUNT_ACTIONTYPE_CALLBACK_MONEY, "回款成功"+ amount + "元");
    }

    @Override
    public void createPayInterestManagerChargeFlow(Account account, BigDecimal amount) {
        createFlow(account, amount, Constants.ACCOUNT_ACTIONTYPE_INTEREST_SHARE, "支付平台利息管理费"+ amount + "元");
    }

    @Override
    public void createSubscribeFlow(Account account, BigDecimal amount) {
        createFlow(account, amount, Constants.ACCOUNT_ACTIONTYPE_SUBSCRIBE_SUCCESS, "认购成功,可用金额减少"+ amount + "元");
    }

    @Override
    public void createTransSuccessFlow(Account account, BigDecimal amount) {
        createFlow(account, amount, Constants.ACCOUNT_ACTIONTYPE_TRANS_SUCCESS, "转让成功,可用金额增加"+ amount + "元");
    }
}
